package finelProject.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import example.AppConstants;
import finelProject.MyAppConstants;

/**
 * Servlet implementation class IncreaseMessageRepublishNumber
 */
public class IncreaseMessageRepublishNumber extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IncreaseMessageRepublishNumber() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			//obtain CustomerDB data source from Tomcat's context
			Context context = new InitialContext();
			BasicDataSource ds = (BasicDataSource)context.lookup(AppConstants.DB_DATASOURCE);
			Connection conn = ds.getConnection();
			
			
			//get the data from the request
			int MessageId  = Integer.parseInt(request.getParameter("MessageId"));
			
			
			//insert the massage into the MESSAGES table in the DB
			PreparedStatement stmt;
			try{
				//get the current message republish number
				stmt = conn.prepareStatement(MyAppConstants.GET_MESSAGE);
				stmt.setInt 	 (1,MessageId);
				ResultSet rs = stmt.executeQuery();
				int RepublishNumber = rs.getInt(3);
				int AutorNumberOfFollowers = rs.getInt(7);
				
				//increase the republish number
				RepublishNumber++;
				
				double popularty = Math.log(2 + AutorNumberOfFollowers)* Math.log(2 + RepublishNumber);
				
				//set the republish number back into the MESSAGES table
				stmt = conn.prepareStatement(MyAppConstants.UPDATE_REPULISH_NUMBER);
				stmt.setInt 	 (1,RepublishNumber);
				stmt.setDouble	 (2, popularty);
				stmt.setInt 	 (3,MessageId);
				stmt.executeUpdate();
				
				//commit update
				conn.commit();
				//close statements
				stmt.close();
			}catch(SQLException e){
				getServletContext().log("Error while INSERT to MESSAGE", e);
				response.sendError(500);//internal server error
			}
			conn.close();
			
		}catch(NamingException | SQLException e){
			e.printStackTrace();
		}
		//TODO: need to send the client side the response
		response.setIntHeader("success", 1);
	}

}
